package cn.com.dhc.zzywb.startspring.demo.controller;

import cn.com.dhc.zzywb.startspring.demo.entity.Account;
import cn.com.dhc.zzywb.startspring.demo.entity.Role;
import cn.com.dhc.zzywb.startspring.demo.entity.User;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@RestController
@RequestMapping("/jdbc")
public class JdbcController {
    //http://localhost:8888/jdbc/userlist
    @Resource

    private JdbcTemplate jdbcTemplate;

    @RequestMapping(value = "/userlist",produces = "application/json;charset=UTF-8")
    public List<Role> getUserList(ModelMap map) {
        String sql = "SELECT * FROM role";
        List<Role> userList = jdbcTemplate.query(sql, new RowMapper<Role>() {
            Role role = null;
            @Override
            public Role mapRow(ResultSet rs, int rowNum) throws SQLException {
                role = new Role();
                role.setId(rs.getInt("id"));
                role.setRole_name(rs.getString("role_name"));

                return role;
            }
        });
        System.out.println("查询成功");
        for (Role user : userList) {
            System.out.println("id = " + user.getId() + " , name = " + user.getRole_name() );
        }
//        System.out.println(userList.toString() );
        System.out.println(userList);
        return userList;
    }

    @GetMapping(value = "/userlist/id/{id}",produces = "application/json;charset=UTF-8")
    public List<Role> getUserid(@PathVariable Integer id) {
        String sql = "SELECT * FROM role where id ="+id;
        List<Role> userList = jdbcTemplate.query(sql, new RowMapper<Role>() {
            Role role = null;
            @Override
            public Role mapRow(ResultSet rs, int rowNum) throws SQLException {
                role = new Role();
                role.setId(rs.getInt("id"));
                role.setRole_name(rs.getString("role_name"));

                return role;
            }
        });
        System.out.println("查询成功");
        for (Role user : userList) {
            System.out.println("id = " + user.getId() + " , name = " + user.getRole_name() );
        }
//        System.out.println(userList.toString() );
        System.out.println(userList);
        return userList;
    }

//    @RequestMapping(value = "/accountlist",produces = "application/json;charset=UTF-8")
//    public List<Account> getaccountList(ModelMap map) {
//        String sql = "SELECT * FROM account";
//        List<Account> accountlist = jdbcTemplate.query(sql, new RowMapper<Role>() {
//            Account account = null;
//            @Override
//            public Account mapRow(ResultSet rs, int rowNum) throws SQLException {
//                account = new Account();
//                account.setId(rs.getInt("id"));
//                account.setUsername(rs.getString("username"));
//
//                return account;
//            }
//        });
//        System.out.println("查询成功");
//        for (Account account : accountlist) {
//            System.out.println("id = " + account.getId() + " , name = " + account.getUsername() );
//        }
////        System.out.println(userList.toString() );
//        System.out.println(accountlist);
//        return accountlist;
//    }
}
